package com.fzz.learning_forum.serviceImpl;

import com.fzz.learning_forum.dao.ArticleMapping;
import com.fzz.learning_forum.domain.Article;
import com.fzz.learning_forum.service.ArticleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vdurmont.emoji.EmojiParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component
@Transactional
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapping articleMapping;

    //分页查询文章
    @Override
    public PageInfo<Article> selectArticleWithPage(Integer page, Integer count) {

        PageHelper.startPage(page,count);
        List<Article> articleList = articleMapping.selectArticleWithPage();
        PageInfo<Article> pageInfo=new PageInfo<>(articleList);
        return pageInfo;
    }

    //根据id查询文章
    @Override
    public PageInfo<Article> selectArticleWithPageById(Integer page, Integer count, Integer id) {
        PageHelper.startPage(page,count);
        List<Article> articleList = articleMapping.selectArticleWithPageById(id);
        PageInfo<Article> pageInfo=new PageInfo<>(articleList);
        return pageInfo;
    }
    //根据tags模糊查询文章
    @Override
    public PageInfo<Article> selectArticleByTags(Integer page, Integer count, String tags) {
        PageHelper.startPage(page,count);
        List<Article> articleList = articleMapping.selectArticleByTags(tags);
        PageInfo<Article> pageInfo=new PageInfo<>(articleList);
        return pageInfo;
    }

    //发布动态
    @Override
    public Integer publishArticle(Article article) {
        //article.setContent(EmojiParser.parseToAliases(article.getContent()));
        Date date=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        simpleDateFormat.format(date);
        article.setCreated(date);
        article.setHit(0);
        return articleMapping.publishArticle(article);
    }

    //根据id查询动态详细信息
    @Override
    public Article selectArticleDetail(Integer id) {
        return articleMapping.selectArticleDetail(id);
    }

    //根据id修改动态
    @Override
    public Integer editArticle(Article article) {
        Date date=new Date();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        simpleDateFormat.format(date);
        article.setModified(date);
        return articleMapping.editArticle(article);
    }

    //根据id删除动态
    @Override
    public Integer deleteArticle(Integer id) {
        return articleMapping.deleteArticle(id);
    }

    //根据id更新动态的热度
    @Override
    public Integer updateArticleHit(Integer id, Integer hit) {
        return articleMapping.updateArticleHit(id,hit);
    }

    //根据热度值排序
    @Override
    public List<Article> selectArticleByHit() {
        return articleMapping.selectArticleByHit();
    }

    //根据id更新动态的浏览量
    @Override
    public Integer updateArticleView(Integer id, Integer view) {
        return articleMapping.updateArticleView(id,view);
    }

    //根据浏览量排序
    @Override
    public List<Article> selectArticleByView() {
        return articleMapping.selectArticleByView();
    }

    @Override
    public List<Article> selectArticleByHitAndView() {
        return articleMapping.selectArticleByHitAndView();
    }
}
